package com.liusy.code.wizards;

import java.util.Arrays;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;

import com.liusy.code.Constant;


/**
 * 表选择
 * @author liusy
 *
 */
@SuppressWarnings( { "restriction" })
public class TableGenerationWizardPage extends AbsWizardPage
{
	List listTalbeSource;
	List listTalbeTarget;
	Button button;
	
	private ISelection selection;

	protected TableGenerationWizardPage(ISelection selection)
	{
		super("Code");
		setTitle("代码生成");
		setDescription("请选择要生成代码的表");
		this.selection = selection;
	}

	@Override
	public void createControl(Composite parent)
	{
		//创建一个新的Composite
		Composite container = new Composite(parent, SWT.NULL);
		//设置网格布局
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;//2列
		layout.verticalSpacing = 9;//垂直间距
		container.setLayout(layout);
		//---------------------------------------------------------------
		
		listTalbeSource = new List(container, SWT.BORDER|SWT.MULTI | SWT.V_SCROLL);
		listTalbeSource.setLayoutData(new GridData(165,250));
		
		
		Composite containerButton = new Composite(container, SWT.NULL);
		containerButton.setLayoutData(new GridData(80,250));
		GridLayout layout1 = new GridLayout();
		layout1.numColumns = 1;
		layout1.verticalSpacing = 9;//垂直间距
		containerButton.setLayout(layout1);
		
		button = new Button(containerButton, SWT.NULL);
		button.setText("-->");
		button.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if(listTalbeSource.getSelectionCount()>0)
				{
					String []  target = listTalbeTarget.getItems();
					Arrays.sort(target);
					for (String item : listTalbeSource.getSelection()) {
						if(Arrays.binarySearch(target, item)==-1)
						{
							listTalbeTarget.add(item);
						}
					}
				}
				if (listTalbeTarget.getItemCount()>0 ) 
				updateStatus(null);
				Constant.tableNameTarget = listTalbeTarget.getItems();
			}
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
			}
		});
		
		
		
		Button button1 = new Button(containerButton, SWT.NULL);
		button1.setText("<--");
		button1.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if(listTalbeTarget.getSelectionCount()>0)
				{
					listTalbeTarget.remove(listTalbeTarget.getSelectionIndices());
				}
				if (listTalbeTarget.getItemCount()>0 )
				updateStatus(null);
				Constant.tableNameTarget = listTalbeTarget.getItems();			}
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
			}
		});
		
		
		
		listTalbeTarget = new List(container,SWT.BORDER|SWT.MULTI | SWT.V_SCROLL);
		listTalbeTarget.setSize(40, 50);
		listTalbeTarget.setLayoutData(new GridData(165,250));
		
		
		
		
		//---------------------------------------------------------------
		
		//设置焦点
		parent.setFocus();
		//初始化界面默认值
		initialize();

		dialogChanged();

		setControl(container);
	}

	/**
	 * 初始化界面默认值
	 */
	public void initialize()
	{
//  		MessageBox mb = new MessageBox(getShell());
//			mb.setMessage( Arrays.toString(Constant.tableNameSourse));
//			mb.open();
		if(null!=Constant.tableNameSourse&&Constant.tableNameSourse.length>0)
		listTalbeSource.setItems(Constant.tableNameSourse);

	}
	
	
	@Override
	protected Object getResult() {
		return this.listTalbeTarget.getItems();
	}

	/**
	 * 验证
	 */
	private void dialogChanged()
	{

        if (listTalbeTarget.getItemCount() ==0 ) {
        	updateStatus("请选择表!");
        	return;
        } 
		updateStatus(null);
	}

}
